Skip to content

BUG: Incomplete Styler copy methods fix (#39708) #39975

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 10 commits into from
Mar 5, 2021

Conversation

rlukevie
Copy link
Contributor

I haven't made so many pull requests before, so I hope this is going into the right direction.

Copy link
Contributor

@attack68 attack68 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

looks like a pretty good start. some comments inline.

na_rep=self.na_rep,
uuid_len=self.uuid_len,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ignore the uuid_len since the uuid is explicitly provided

@@ -783,15 +783,29 @@ def _copy(self, deepcopy: bool = False) -> Styler:
precision=self.precision,
caption=self.caption,
uuid=self.uuid,
Copy link
Contributor

@attack68 attack68 Feb 22, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the uuid is suffixed with "_" so need to trim this off here self.uuid[:-1]
edit: actually it might be cleaner and always work if the uuid is manually overwritten after class instantiation.

@@ -482,6 +482,7 @@ Other
- Bug in :class:`Styler` where rendered HTML was missing a column class identifier for certain header cells (:issue:`39716`)
- Bug in :meth:`Styler.background_gradient` where text-color was not determined correctly (:issue:`39888`)
- Bug in :meth:`DataFrame.equals`, :meth:`Series.equals`, :meth:`Index.equals` with object-dtype containing ``np.datetime64("NaT")`` or ``np.timedelta64("NaT")`` (:issue:`39650`)
- Bug in :class:`Styler` copy methods which were not updated for recently introduced attributes (:issue:`39708`)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can move this up to where other Styler Items are listed

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this may not be necessary if this changes were in 1.3 (which they might be); you can add this issue number with another change for Styler (e.g. where the attributes were changed)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this has to be 1.3, since tooltips is the most recent and thats in 1.3.0, referenced in this PR

if deepcopy:
styler.ctx = copy.deepcopy(self.ctx)
styler._todo = copy.deepcopy(self._todo)
styler.table_styles = copy.deepcopy(self.table_styles)
styler.hidden_columns = copy.deepcopy(self.hidden_columns)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

think hidden_columns is a list, does it need deepcopy?

)
self.styler.set_tooltips(ttips)
self.styler.cell_ids = not self.styler.cell_ids
self.styler.format("{:.2%}")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not making any changes to _todo[]. maybe add .apply(..)

self.styler.table_styles[0]["selector"] = "ti"
assert self.styler.table_styles != s2.table_styles
if do_changes: # self.styler.tooltips is not None
tm.assert_frame_equal(self.styler.tooltips.tt_data, s2.tooltips.tt_data)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

possible to wrap the two test cases copy and deepcopy so that dont repeat so much code?


assert self.styler.table_styles is s2.table_styles
assert self.styler.hidden_columns is s2.hidden_columns
assert self.styler.cell_context is s2.cell_context
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can you group the shallow variables in test to make it clearer?

assert self.styler.table_styles == s2.table_styles
assert self.styler.table_attributes == s2.table_attributes
assert self.styler.cell_ids == s2.cell_ids
assert self.styler.uuid_len == s2.uuid_len
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

uuid is the key variable, uuid_len is only used in construction of uuid test for that instead.

@@ -482,6 +482,7 @@ Other
- Bug in :class:`Styler` where rendered HTML was missing a column class identifier for certain header cells (:issue:`39716`)
- Bug in :meth:`Styler.background_gradient` where text-color was not determined correctly (:issue:`39888`)
- Bug in :meth:`DataFrame.equals`, :meth:`Series.equals`, :meth:`Index.equals` with object-dtype containing ``np.datetime64("NaT")`` or ``np.timedelta64("NaT")`` (:issue:`39650`)
- Bug in :class:`Styler` copy methods which were not updated for recently introduced attributes (:issue:`39708`)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this may not be necessary if this changes were in 1.3 (which they might be); you can add this issue number with another change for Styler (e.g. where the attributes were changed)

@attack68 attack68 added this to the 1.3 milestone Feb 23, 2021
@rlukevie
Copy link
Contributor Author

When committing my changes based on your comments, do I always have to add 'BUG: ... (#39708)' to my commit messages? Or is it okay if I just write something like 'Mention issue number in enhancements section'?

I prefer atomic commits, but do all my commits end up upstream after the PR is merged? Shall I do only one single commit for all my changes instead?

@attack68 attack68 added Styler conditional formatting using DataFrame.style Copy / view semantics labels Feb 23, 2021
@attack68
Copy link
Contributor

When committing my changes based on your comments, do I always have to add 'BUG: ... (#39708)' to my commit messages? Or is it okay if I just write something like 'Mention issue number in enhancements section'?

I prefer atomic commits, but do all my commits end up upstream after the PR is merged? Shall I do only one single commit for all my changes instead?

when merged to master will be squashed to one commit. do as many commits as you want, when you need a new review generally ping for one, wait patiently, or request here. :)

@rlukevie
Copy link
Contributor Author

when merged to master will be squashed to one commit. do as many commits as you want, when you need a new review generally ping for one, wait patiently, or request here. :)

Thanks, and good to know!

@rlukevie
Copy link
Contributor Author

Hi, here's my next try!

@@ -781,16 +781,29 @@ def _copy(self, deepcopy: bool = False) -> Styler:
self.data,
precision=self.precision,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is there a reason we actually care about deepcopy, IOW why are we not just always a shallow copy? is this actually used somewhere?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Personally, I have never had use to copy a Styler. But I can see a use case where, in a notebook or report, you might have a dataframe that has some preliminary, shared styling and then some divergence where you highlight different properties as part of a narrative.

I think, off the top of my head, in this scenario you would need a deepcopy to prevent any updates from intermingling with each other, since much of the core styling I think comes from the attributes where the shallow copy will have only single shared pointed reference.

Copy link
Contributor

@attack68 attack68 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm.

leaving the discussion about whether copy and/or deepcopy are needed, this I think fixes the existing functionality which has been broken by most of the recent changes.

Copy link
Contributor

@jreback jreback left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm. @rlukevie can you merge master and ping on green

@jreback jreback merged commit 5dcaf49 into pandas-dev:master Mar 5, 2021
@jreback
Copy link
Contributor

jreback commented Mar 5, 2021

thanks @rlukevie

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Copy / view semantics Styler conditional formatting using DataFrame.style
Projects
None yet
Development

Successfully merging this pull request may close these issues.

BUG: Styler copy methods not updated for recent additions
3 participants